/*
Copyright (c) 2009 Hayden Curran <haydenz@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    * The names of its contributors may not be used to endorse or promote
        products derived from this software without specific prior
        written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "server_log.h"

/*
    Binds the server log UDP socket to port SERVER_LOG_PORT.
    Returns the file descriptor.
    Or on error -1.
*/
int server_log_bind(void) {
	int slfd;
	struct addrinfo hints, *servinfo, *p;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_PASSIVE; // use my IP

	getaddrinfo(NULL, SERVER_LOG_PORT, &hints, &servinfo);

	// loop through all the results and bind to the first we can
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((slfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("server log socket");
			continue;
		}

		if (bind(slfd, p->ai_addr, p->ai_addrlen) == -1) {
			closesocket(slfd);
			perror("server log bind");
			continue;
		}

		break;
	}

	if (p == NULL) {
		fprintf(stderr, "server log failed to bind socket\n");
		return -1;
	}

	freeaddrinfo(servinfo);

    return slfd;
}

/*
    Receives data from slfd socket.
    Then parses the data.
    Assumes that it received one full packet of data.
    Returns the size of the data received.
    On error returns -1.
*/
int server_log_receive(int slfd, int ircfd, pug_t *pug) {
    char buf[4096];
    int ret, length;
    struct sockaddr_in server;
    char *data;

    ret = recvfrom(slfd, buf, 4096, 0, (void *)&server, &length);
    if (ret == -1) {
        perror("server log recvfrom");
        return -1;
    }
    buf[ret] = 0;

    if (buf[0] == -1 && buf[3] == -1) data = buf + 4;

    server_log_parse(data, ret-4, ircfd, pug);

    return ret;
}

/*
    Parses data and if needed relays it back to irc
*/
int server_log_parse(char *data, int len, int ircfd, pug_t *pug) {

    printf("%s", data);
    return 1;
}
